layui.use(['form','treeSelect','layer','upload','laydate', 'layedit'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;
    var laydate = layui.laydate;
    var layedit = layui.layedit;

    //创建一个富文本编辑器
    var editIndex = layedit.build('articleContent',{
        height : 535,
        tool: [
            'strong' //加粗
            ,'italic' //斜体
            ,'underline' //下划线
            ,'del' //删除线
            ,'|' //分割线
            ,'left' //左对齐
            ,'center' //居中对齐
            ,'right' //右对齐
            ,'|' //分割线
            ,'link' //超链接
            ,'unlink' //清除链接
            ,'face' //表情
            ,'image' //插入图片
        ],
        uploadImage : {
            url : "/layedit/upload/pic"
        }
    });


    //图片上传 field: 'uploadFile',//上传文件的名称，要和接口参数名称相同
    upload.render({
        elem: '#articleCoverUpload',
        url: '/upload/pic',
        accept: 'images',
        acceptMime: 'image/*',
        field: "uploadFile", //设定文件域的字段名
        before: function(obj){
        },
        done: function(res){
            //上传完毕
            var url = res.url;
            $('#articleCover').val(url);
            $('.articleCoverImg').attr('src', res.url);
        }
    });

    //物种树形下拉框信息
    treeSelect.render({
        elem: '#tree-select-resourceTypeId',
        data: '/api/agt_resource_type/tree/select/ef29c5bd67064812b94b921695a962ef',
        type: 'get',
        placeholder: '请选择',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select-resourceTypeId").val(data.current.id);
        },
        success: function (item) { // 加载完成后的回调函数 areaId
            $("#tree-select-resourceTypeId").val("");
        }
    });

    form.verify({
        content: function(value) {
            return layedit.sync(editIndex);
        }
    });
    //添加场景信息
    form.on('submit(add)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/api/agt_article/add',
            data:data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});